//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CSimpleGeometry10.cpp>
///	@path	~/src/graphics/dx10/
///	@date	2008/12/28
///	@desc	.

#include "config/config.h"

#include "graphics/dx10/CSimpleGeometry10.h"

namespace xeres {
	
	IMPL_IOBJECT_CLASS( CSimpleGeometry10 );

	// ctor
	CSimpleGeometry10::CSimpleGeometry10( Ref<CVertexLayout10> layout , size_t stride , size_t vertex_count , size_t count ,
		D3D10_PRIMITIVE_TOPOLOGY type , ID3D10Buffer * vertex , ID3D10Buffer * index )
		: m_layout( layout )
		, m_stride( (ushort)stride )
		, m_count( count )
		, m_vertexCount( vertex_count)
		, m_type( type )
		, m_pVertexBuffer( vertex )
		, m_pIndexBuffer( index )
	{
	}

	// dtor
	CSimpleGeometry10::~CSimpleGeometry10( void )
	{
		if( m_pVertexBuffer )
		{
			UINT rest = m_pVertexBuffer->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CSimpleGeometry10::DTOR: Invalid vertex buffer reference count.") );
				FATAL_ABORT( _S("Invalid ID3D10Buffer reference count.") );
			}
		}

		if( m_pIndexBuffer )
		{
			UINT rest = m_pIndexBuffer->Release();
			if( rest != 0 )
			{
				TRACE_ERROR( _S("CSimpleGeometry10::DTOR: Invalid index buffer reference count.") );
				FATAL_ABORT( _S("Invalid ID3D10Buffer reference count.") );
			}
		}
	}
	// GetLayout
	RefWeak<IVertexLayout> CSimpleGeometry10::GetLayout( void )
	{
		return m_layout;
	}
	// EstablishBuffer
	void CSimpleGeometry10::EstablishBuffer( RenderContext& rc )
	{
		// should plug stream by shader
		uint offset[2] = {0,0};
		uint stride[2] = {m_stride,0};
		rc.GetDX10()->IASetVertexBuffers( 0 , 1 , &m_pVertexBuffer , stride , offset );
		if( m_pIndexBuffer )
		{
			rc.GetDX10()->IASetIndexBuffer( m_pIndexBuffer , DXGI_FORMAT_R16_UINT , 0 );
		}
		rc.GetDX10()->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY(m_type) );
	}

	// draw geometry
	void CSimpleGeometry10::Draw( RenderContext& rc )
	{
		rc.GetDX10()->Draw( m_vertexCount , 0 );
	}
	// OnLostDevice
	bool CSimpleGeometry10::OnLostDevice( void )
	{
		return false;
	}
	// OnResetDevice
	bool CSimpleGeometry10::OnResetDevice( RefWeak<IDevice> device )
	{
		return false;
	}

} // namespace xeres
